package com.atguigu.tingshu.search.config;

import com.atguigu.tingshu.search.propreties.ThreadPoolProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.*;

@Configuration
public class ThreadPoolExecutorConfiguration {

    @Autowired
    private ThreadPoolProperties threadPoolProperties;

    @Bean
    public ThreadPoolExecutor threadPoolExecutor(){
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
        threadPoolProperties.getCorePoolSize(),
        threadPoolProperties.getMaximumPoolSize(),
        threadPoolProperties.getKeepAliveTime(),
        TimeUnit.SECONDS,
        new ArrayBlockingQueue<>(threadPoolProperties.getWorkQueueSize()),
        Executors.defaultThreadFactory(),
        new ThreadPoolExecutor.AbortPolicy()
        );
        return threadPoolExecutor;

    }
}